package com.cgw.game.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.cgw.game.R;
import com.cgw.game.view.MyLineChart;
import com.cgw.game.view.MyLineChartRenderer;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.DefaultFillFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DoubleLineActivity extends AppCompatActivity {
    private MyLineChart line_chart;
    String[] titles = new String[]{"清醒","浅睡","深睡","小憩","活动"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//返回
            }
        });
        line_chart = findViewById(R.id.line_chart);
        initLine();
        List<Entry> values = new ArrayList<>();
        List<Entry> values1 = new ArrayList<>();
        Random ra = new Random();
        for (int i = 0; i < 30; i++) {
            values.add(new Entry(i, ra.nextInt(180) + 40));
        }
        values.get(6).setY(0f);
        values.get(15).setY(0f);


        values1.add(new Entry(0, 30));
        values1.add(new Entry(3, 50));
        values1.add(new Entry(6, 30));
        values1.add(new Entry(7, 50));
        values1.add(new Entry(8, 10));
        values1.add(new Entry(12, 30));
        values1.add(new Entry(14, 10));
        values1.add(new Entry(15, 40));
        values1.add(new Entry(20, 50));
        values1.add(new Entry(23, 20));
        values1.add(new Entry(25, 40));
        values1.add(new Entry(26, 10));
        values1.add(new Entry(27, 50));
        values1.add(new Entry(29, 30));
        setData(values,values1);
    }

    private void initLine() {
        line_chart.setExtraBottomOffset(5f);
//        chart3Line.getAxisLeft().setDrawGridLines(false);
        line_chart.setTouchEnabled(true);
        line_chart.setPinchZoom(false);
        line_chart.setScaleYEnabled(false);
        line_chart.setDragEnabled(true);
        line_chart.setDoubleTapToZoomEnabled(false);
        line_chart.getViewPortHandler().setMaximumScaleX(5.0f);
        //取消说明
        line_chart.getLegend().setEnabled(false);
        line_chart.getDescription().setEnabled(false);
        line_chart.setNoDataText("暂无数据");


        YAxis leftAxis = line_chart.getAxisLeft();
        leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        leftAxis.setTextSize(12f);
        leftAxis.setTextColor(Color.parseColor("#91a2a3"));
//      leftAxis.setValueFormatter(new PercentFormatter());
//        leftAxis.setValueFormatter(new IndexAxisValueFormatter());
        leftAxis.setEnabled(true);
        leftAxis.setGridColor(Color.parseColor("#e3edf2"));
        leftAxis.setDrawAxisLine(false);
        leftAxis.setAxisMinimum(40f);
        leftAxis.setAxisMaximum(220f);
        /*leftAxis.addLimitLine(new LimitLine(125));
        leftAxis.addLimitLine(new LimitLine(147));
        leftAxis.addLimitLine(new LimitLine(188));*/
        leftAxis.setLabelCount(5, true);



        YAxis rightAxis = line_chart.getAxisRight();
        rightAxis.setTextColor(Color.RED);
        rightAxis.setAxisMaximum(55);
        rightAxis.setAxisMinimum(0);
        rightAxis.setDrawGridLines(false);
        rightAxis.setGranularityEnabled(false);
        rightAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int index = (int)(value/10)-1;
                if(index<0||index>4){
                    return "";
                }
                return titles[index];
            }
        });


        //横向轴线设置
        XAxis xAxis = line_chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(12f);
        xAxis.setTextColor(Color.parseColor("#91a2a3"));
        xAxis.setEnabled(true);  //是否打开x轴
        xAxis.setDrawGridLines(false);
        xAxis.setAxisMaximum(35f);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularityEnabled(true);
        xAxis.setLabelCount(5, false);
        // 转换要显示的标签文本，value值默认是int从0开始
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return famatTime((int) value);
            }
        });
        xAxis.setAxisLineColor(Color.parseColor("#e3edf2"));
        //设置为虚线
        xAxis.setDrawAxisLine(true);
    }
    private String famatTime(int value) {
        int min = value * 5;
        String m = min / 60 + "";
        if (min / 60 < 10) {
            m = "0" + min / 60;
        }
        String s = min % 60 + "";
        if (min % 60 < 10) {
            s = "0" + min % 60;
        }
        return m + ":" + s;
    }
    private void setData(List<Entry> values,List<Entry> values1) {
        line_chart.clear();

        if (line_chart.getRenderer() instanceof MyLineChartRenderer) {
            MyLineChartRenderer renderer = (MyLineChartRenderer) line_chart.getRenderer();

            int medium = 125;
            int larger = 147;
            int limit = 188;


            int[] colors = new int[4];
            colors[0] = Color.parseColor("#fa7069");
            colors[1] = Color.parseColor("#faa369");
            colors[2] = Color.parseColor("#facd69");
            colors[3] = Color.parseColor("#d3d8dc");

            renderer.setHeartLine(true, medium,larger,limit, colors);
        }


        LineDataSet lineDataSet;
        lineDataSet = new LineDataSet(values, "心率");
        lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lineDataSet.setColor(getResources().getColor(R.color.red));
        lineDataSet.setCircleColor(getResources().getColor(R.color.red));
        lineDataSet.setDrawHorizontalHighlightIndicator(false);
        lineDataSet.setHighLightColor(getResources().getColor(R.color.red));
        lineDataSet.enableDashedHighlightLine(10, 10, 0);
        lineDataSet.setFillDrawable(getResources().getDrawable(R.drawable.line_chart_bg));
        lineDataSet.setFillAlpha(100);
        lineDataSet.setDrawFilled(false);
        lineDataSet.setHighlightLineWidth(1f);
        lineDataSet.setLineWidth(2f);
        lineDataSet.setCircleRadius(3f);
        lineDataSet.setDrawCircleHole(false);
        lineDataSet.setDrawCircles(false);
        lineDataSet.setDrawValues(false);

        LineDataSet lineDataSet1;
        lineDataSet1 = new LineDataSet(values1, "睡眠");
        lineDataSet1.setAxisDependency(YAxis.AxisDependency.RIGHT);
        lineDataSet1.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lineDataSet1.setColor(getResources().getColor(R.color.red));
        lineDataSet1.setCircleColor(getResources().getColor(R.color.red));
        lineDataSet1.setDrawHorizontalHighlightIndicator(false);
        lineDataSet1.setHighLightColor(getResources().getColor(R.color.red));
        lineDataSet1.enableDashedHighlightLine(10, 10, 0);
        lineDataSet1.setFillDrawable(getResources().getDrawable(R.drawable.line_chart_bg));
        lineDataSet1.setFillAlpha(100);
        lineDataSet1.setDrawFilled(false);
        lineDataSet1.setHighlightLineWidth(1f);
        lineDataSet1.setLineWidth(2f);
        lineDataSet1.setCircleRadius(3f);
        lineDataSet1.setDrawCircleHole(false);
        lineDataSet1.setDrawCircles(false);
        lineDataSet1.setDrawValues(false);
        LineData lineData = new LineData(lineDataSet1,lineDataSet);
        line_chart.setData(lineData);
    }
}
